/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package windelpdv;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Servidor
 */
public class IniciaCaixa extends javax.swing.JFrame {

    private Connection ConexaoPDVWindel = null;
     Dimension tela = Toolkit.getDefaultToolkit().getScreenSize();
     
    /**
     * Creates new form IniciaCaixa
     */
    public IniciaCaixa(Connection conexaoPDVWindel) {
        this.ConexaoPDVWindel = conexaoPDVWindel;
        initComponents();
        //setSize(600, 600); //ponha aqui suas medidas  
        setLocation((tela.width - this.getSize().width) / 2, (tela.height - this.getSize().height) / 2);
        Date data = new Date(System.currentTimeMillis());
        SimpleDateFormat formatarDate = new SimpleDateFormat("dd/MM/yyyy");        
        tfData.setText(formatarDate.format(data));
        tfValorInicio.grabFocus();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfData = new javax.swing.JTextField();
        try{  
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("##/##/####");  
            tfData = new javax.swing.JFormattedTextField(data);  
        }  
        catch (Exception e){  
        }
        tfValorInicio = new javax.swing.JTextField();
        tfValorInicio = new JMoneyField();
        btOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Abrir Caixa");

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel1.setText("Abertura de Caixa");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel2.setText("Data:");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel3.setText("Valor de Inicio:");

        tfData.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N

        tfValorInicio.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        tfValorInicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfValorInicioActionPerformed(evt);
            }
        });

        btOK.setText("OK");
        btOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tfData, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                            .addComponent(tfValorInicio))
                        .addGap(26, 26, 26)
                        .addComponent(btOK)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfValorInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btOK))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfValorInicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfValorInicioActionPerformed
          Statement st = null;
          int dia = Integer.parseInt(tfData.getText().substring(0, 2));
            int mes = Integer.parseInt(tfData.getText().substring(3, 5));
            int ano = Integer.parseInt(tfData.getText().substring(6, 10));
        
             try {
                    
                    st = ConexaoPDVWindel.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                                    
                    
                    
                   st.execute("insert into MOVIMENTACAO (DATA, VALOR, TIPO) values ("+
                                "\'" + ano+"-"+mes+"-"+dia + "\',"+
                                 tfValorInicio.getText().replace(",", ".") + ","+
                                "\'" + "ENTRADA" + "\')" );
                   tfData.setText("");
                   tfValorInicio.setText("");
                   JOptionPane.showMessageDialog(null, "Valor lançado com sucesso!", "Atenção", JOptionPane.INFORMATION_MESSAGE);
                   this.dispose();
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(null, "Registros não inseridos!", "Erro", JOptionPane.INFORMATION_MESSAGE);
                }
                        
        /*  Statement st = null;
            ResultSet res = null;
            int dia = Integer.parseInt(tfData.getText().substring(0, 2));
            int mes = Integer.parseInt(tfData.getText().substring(3, 5));
            int ano = Integer.parseInt(tfData.getText().substring(6, 10));
            try {
                    
                    st = ConexaoPDVWindel.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    res = st.executeQuery("SELECT MAX(COD_CUPOM) FROM FECHA_CAIXA");
                    res.first();
                    int Cod_cupom = res.getInt("MAX");
                    Cod_cupom = Cod_cupom + 1;
                    
                   st.execute("insert into FECHA_CAIXA (DATA, DINHEIRO, CHEQUE, CREDIARIO, COD_CUPOM, TOTAL_CUPOM) values ("+
                                "\'" + ano+"-"+mes+"-"+dia + "\',"+
                                 + 1 + ","+
                                 + 0 + ","+
                                 + 0 + ","+
                                 + Cod_cupom + ","+
                                 tfValorInicio.getText().replace(",", ".") + ")" );
                   tfData.setText("");
                   tfValorInicio.setText("");
                   JOptionPane.showMessageDialog(null, "Valor lançado com sucesso!", "Atenção", JOptionPane.INFORMATION_MESSAGE);
                   this.dispose();
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(null, "Registros não inseridos!", "Erro", JOptionPane.INFORMATION_MESSAGE);
                }*/
    }//GEN-LAST:event_tfValorInicioActionPerformed

    private void btOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOKActionPerformed
        tfValorInicioActionPerformed(evt);        
    }//GEN-LAST:event_btOKActionPerformed

    /**
     * @param args the command line arguments
     */
   public String formatarFloat(float numero) {
        String retorno = "";
        DecimalFormat formatter = new DecimalFormat("0.00");
        try {
            retorno = formatter.format(numero);
        } catch (Exception ex) {
            System.err.println("Erro ao formatar numero: " + ex);
        }
        return retorno;
    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField tfData;
    private javax.swing.JTextField tfValorInicio;
    // End of variables declaration//GEN-END:variables
}
